﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections;
using System.Collections.Concurrent;
using System.Windows.Input;

namespace SUNRPA.CONTROLS.IMPL.controls {
    public class TextCut2ListNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            TextCut2ListNParam? param = item.ControlParam as TextCut2ListNParam;
            string inputText = param.InputText;
            List<String> specialKey = this.GetStringVar(ref inputText, globleData);
            if(specialKey.Count > 0) { 
                throw new RpaRuntimeException(string.Format("分割字符串变量{0}格式不符合规范", param.InputText));
            }
            int separation = param.Separation;
            bool filterEmpty = param.FilterEmpty;
            List<string> result = new List<string>();
            if(separation == 0) {
                //标准分隔符
                int separator = param.StanderSepType;
                char separatorChar = ' ';
                if(separator == (int)StanderSeparatorType.SPACE) { 
                    separatorChar = ' ';
                }else if(separator == (int)StanderSeparatorType.TAB) {
                    separatorChar = '\t';
                } else { 
                    separatorChar = '\n';
                }
                if(filterEmpty) { 
                    result = inputText.Split(new char[] {separatorChar}, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
                } else { 
                    result = inputText.Split(separatorChar).ToList<string>();
                }
            } else { 
                //自定义分隔符(暂时不做)
            }
            this.saveVar(globleData, param.OutListName, new ListModel<string>(param.OutListName, result), DefaultContents.RUNNING_PARAM);
            return currentIndex;
        }
    }
}
